import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:test01/okhttp_test/page/pages/okhttp_main.dart';
import 'package:test01/test/time/time_pojo.dart';


/// 动画点击变大
void main()=>runApp(pages());

class pages extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: new TimePage(),
    );
  }
}

class TimePage extends StatefulWidget {
  const TimePage({Key key}) : super(key: key);

  @override
  _TimePageState createState() => _TimePageState();
}

class _TimePageState extends State<TimePage> {
  //控制器
  ScrollController controller=ScrollController();
  //自定义时间对象
  List<TimePojo> timeList=[];
  //选中的下标
  int timeIndex;

  int newWeeks;
  int newNumbers;
  int newYears;
  int newMonths;
  ///获取循环时间
  void times(){
    //获取时间
    DateTime dateTime= DateTime.now();
    newNumbers=dateTime.day;
    newWeeks=dateTime.weekday;
    newYears=dateTime.year;
    newMonths=dateTime.month;
    //获取天数
    var olddayCount = DateTime(newYears, newMonths , 0).day;
    var dayCount = DateTime(newYears, newMonths+1, 0).day;
    //初始化月份和号数
    var j=newNumbers;
    var months=newMonths-1;


    ///获取：年-月-日
    for(int i=0;i<((olddayCount-newNumbers)+dayCount+newNumbers);i++){
      //当前月
      if((i==olddayCount-newNumbers+1)){
        j=1;
        months=newMonths;
      }
      //下个月
      if((i==(olddayCount-newNumbers)+dayCount)){
        j=1;
        months=newMonths+1;
      }
      TimePojo timePojo=TimePojo(year: newYears,month:months,day: j);
      timeList.add(timePojo);
      j++;
    }
    ///获取：周数
    for(int i=0;i<timeList.length;i++){

      String timerDays=timeList[i].day<10? "0${timeList[i].day}":"${timeList[i].day}";
      String timerMonts=timeList[i].month<10? "0${timeList[i].month}":"${timeList[i].month}";

      //字符串转DateTiem
      String _time = "$newYears-$timerMonts-$timerDays";
      DateTime _strtimes = DateTime.parse(_time );
      switch(_strtimes.weekday){
        case 1:
          timeList[i].week="周一";
          break;
        case 2:
          timeList[i].week="周二";
          break;
        case 3:
          timeList[i].week="周三";
          break;
        case 4:
          timeList[i].week="周四";
          break;
        case 5:
          timeList[i].week="周五";
          break;
        case 6:
          timeList[i].week="周六";
          break;
        case 7:
          timeList[i].week="周日";
          break;
      }
      if((timeList[i].day==newNumbers)&&(timeList[i].month==newMonths)){
        timeList[i].week="今天";
      }
      //DateTime转时间戳
      // var _intendtime = _strtimes .millisecondsSinceEpoch;
    }
  }


  @override
  void initState() {
    // TODO: implement initState
    times();
    super.initState();

    //在界面渲染完毕之后调用
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      for(int i=0;i<timeList.length;i++){
        if(timeList[i].week=="今天"){
          //定位到今天
          print("-------------------$i,${((MediaQuery.of(context).size.width)/7)*i}---------------------------");
          if(controller!=null) {
            controller.jumpTo(((MediaQuery
                .of(context)
                .size
                .width) / 7) * i);
          }
          timeIndex=i;
        }
      }
    });
  }




  @override
  Widget build(BuildContext context) {
    ///获取：周数

    return Scaffold(
      appBar: AppBar(
        title: Text("时间选择"),
      ),
      body: ListView.builder(
        //定位到当天
        controller: controller,

        scrollDirection: Axis.horizontal,
        reverse: false,
        itemBuilder: (BuildContext context, int i) {
          return GestureDetector(
            onTap: (){
              timeIndex=i;
              print("选中的日期是：${timeList[i].year}-${timeList[i].month}-${timeList[i].day}");
              setState(() {
              });
            },
            child: Container(
              width:(MediaQuery.of(context).size.width)/7,
              height: 20,
              child: Column(
                children: <Widget>[
                  Container(
                    child:timeIndex==i?Text("${timeList[i].week}",style: TextStyle(fontSize: 19,color: Colors.blue),):
                    Text("${timeList[i].week}",style: TextStyle(fontSize: 17),),
                  ),
                  Container(
                    child: timeIndex==i?Text("${timeList[i].day}",style: TextStyle(fontSize: 18,color: Colors.blue),):
                    Text("${timeList[i].day}",style: TextStyle(fontSize: 16),),
                  )
                ],
              ),
            )
          );
        },

        itemCount: timeList.length,
      )
    );
  }
}
